/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;
import java.io.File;

/**
 *
 * @author 叶勤勇
 */
public class StoreHelper {
    private Environment env;
    private EntityStore store;
    
    public StoreHelper(String envFile, String storeName) throws DatabaseException {
        this(envFile, storeName, false);
    }
    
    public StoreHelper(String envFile, String storeName, boolean readonly) throws DatabaseException {
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(!readonly);
        envConfig.setReadOnly(readonly);
        env = new Environment(new File(envFile), envConfig);
        
        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setAllowCreate(!readonly);
        storeConfig.setReadOnly(readonly);
        store = new EntityStore(env, storeName, storeConfig);
    }
    
    public EntityStore getStore() {
        return store;
    }
    
    public void close() throws DatabaseException {
        if(store != null) {
            store.close();
            store = null;
        }

        if(env != null) {
            env.close();
            env = null;
        }
        
    }
}
